package com.yanqu.road.server.manger.maidian.wineroom;

import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.winery.WineryConfig;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.extension.ExtensionWineryItemInfo;
import com.yanqu.road.entity.winery.UserWineryData;
import com.yanqu.road.entity.winery.UserWineryDrinkerData;
import com.yanqu.road.entity.winery.UserWineryInfoData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.winery.WineryModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.PatronsMgr;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.WineryConfigMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WineryRoomLog {

    public static final String WINE_ROOM = "wine_room";

    public static final String WINE_SKILL = "wine_skill";
    public static final String WINE_MENU = "wine_menu";
    public static final String WINE_STORY = "wine_story";
    public static final String WINE_LEVEL = "wine_level";

    public static final String WINE_TYPE = "wine_type";
    public static final String WINE_SKILLNAME = "wine_skillname";
    public static final String WINE_SKILLLV = "wine_skilllv";
    public static final String VOCATION = "vocation";
    public static final String WINE_SKILLADD = "wine_skilladd";

    public static final String WINE_NAME = "wine_name";
    public static final String WINE_WINELV = "wine_winelv";
    public static final String WINE_ADD = "wine_add";

    public static final String WINE_ROLENAME = "wine_rolename";
    public static final String WINE_LV = "wine_lv";
    public static final String WINE_NUM = "wine_num";


    public static void uploadSnapshot(GamePlayer player) {

        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            DefaultEventTraceAction action = new DefaultEventTraceAction(WINE_ROOM, player);

            WineryModule wineryModule = player.getModule(WineryModule.class);

            UserWineryInfoData userWineryInfoData = wineryModule.getUserWineryInfoData();

            if(userWineryInfoData == null){
                return;
            }

            action.build(WINE_LEVEL, userWineryInfoData.getBadgeLevel());

            List<Map<String, Object>> objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, UserWineryData> dataEntry : wineryModule.getUserWineryDataMap().entrySet()) {
                UserWineryData userWineryData = dataEntry.getValue();
                WineryConfig wineryConfig = WineryConfigMgr.getWineryConfig(userWineryData.getWineryId());
                if(wineryConfig == null){
                    continue;
                }
                Map<String, Object> map = new HashMap<>();
                map.put(WINE_TYPE, wineryConfig.getType());
                for (int i = 0; i < wineryConfig.getSkillList().size(); i++) {
                    if(i == 2){
                        int skillId = wineryConfig.getSkillList().get(i);
                        SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
                        if (skillInfo == null) {
                            continue;
                        }
                        map.put(WINE_SKILLNAME, skillId);
                        map.put(WINE_SKILLLV, userWineryData.getLevel());
                        map.put(VOCATION, skillInfo.getParamList().get(2));
                        map.put(WINE_SKILLADD, SkillMgr.getSkillAddition(skillInfo, userWineryData.getLevel()));
                    }
                }
                objectGroup.add(map);
            }
            if(objectGroup.size() > 0) {
                action.build(WINE_SKILL, objectGroup);
            }

            //名酒记
            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, ExtensionWineryItemInfo> infoEntry : wineryModule.getUserWineryItemInfoMap().entrySet()) {
                ExtensionWineryItemInfo itemInfo = infoEntry.getValue();
                Map<String, Object> map = new HashMap<>();
                map.put(WINE_NAME, itemInfo.getId());
                map.put(WINE_WINELV, itemInfo.getLevel());
                SkillInfo skillInfo = SkillMgr.getSkillWith(eSkillType.PatronsCookSkill.getValue(), itemInfo.getId());
                if (skillInfo != null) {
                    if (skillInfo.getType() == eSkillType.PatronsCookSkill.getValue()) {
                        int occupation = skillInfo.getParamList().get(2);
                        int addValue = SkillMgr.getSkillAddition(skillInfo, itemInfo.getLevel());
                        map.put(VOCATION, occupation);
                        map.put(WINE_ADD, addValue);
                    }
                }
                objectGroup.add(map);
            }
            if(objectGroup.size() > 0) {
                action.build(WINE_MENU, objectGroup);
            }

            List<Integer> list = new ArrayList<>();
            String[] paramArr = GameConfig.WINERY_FRIENDLY_REWARD.split("\\|");
            for(String param : paramArr) {
                String[] dataArr = param.split("#");
                if (dataArr.length > 1) {
                    int friendlyValue = Integer.parseInt(dataArr[0]);
                    list.add(friendlyValue);
                }
            }
            //酒客故事
            objectGroup = new ArrayList<>();
            for (Map.Entry<Integer, UserWineryDrinkerData> dataEntry : wineryModule.getUserWineryDrinkerDataMap().entrySet()) {
                UserWineryDrinkerData drinkerData = dataEntry.getValue();
                Map<String, Object> map = new HashMap<>();
                map.put(WINE_ROLENAME, drinkerData.getDrinkerId());
                for (int i = 0; i < list.size(); i++) {
                    int val = list.get(i);
                    if(drinkerData.getFriendlyValue() <= val){
                        map.put(WINE_LV, i);
                        break;
                    }
                }
                PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(drinkerData.getDrinkerId());
                if(patronsInfo != null){
                    map.put(VOCATION, patronsInfo.getOccupation());
                }
                map.put(WINE_NUM, drinkerData.getFriendlyValue());
                objectGroup.add(map);
            }
            if (objectGroup.size() > 0) {
                action.build(WINE_STORY, objectGroup);
            }
            action.trace();
        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }
}
